/**
 * Copyright (c) 2010 BullionXpress.com,
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */

package com.bullionxpress.server.application;

import java.sql.Connection;
import java.sql.Statement;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Vector;
import java.util.concurrent.LinkedBlockingQueue;
import com.bullionxpress.client.view.ScrollBarPanel;
import com.bullionxpress.server.impl.AccountRegistrar;
import com.bullionxpress.server.impl.DatabaseManager;
import com.bullionxpress.server.impl.InstrumentManager;
import com.bullionxpress.server.impl.OrderManager;
import com.bullionxpress.server.impl.PositionManager;
import com.bullionxpress.server.impl.QuoteManager;
import com.bullionxpress.server.impl.TickManager;
import com.bullionxpress.server.impl.TransactionManager;
import com.bullionxpress.server.impl.UserManager;
import com.bullionxpress.server.impl.UserRegistrar;
import com.bullionxpress.shared.application.SharedGlobals;
import com.bullionxpress.shared.util.Log;

/**
 *
 * @author BullionXpress.com
 */
public class ServerGlobals {
    private static OrderManager orderManager = null; //new OrderManager();
    private static QuoteManager quoteManager = null; //new QuoteManager();
    private static TransactionManager transactionManager = null; //new QuoteManager();
    private static PositionManager positionManager = null; //new PositionManager();
    private static TickManager tickManager = null; //new PositionManager();
    static {
        try {

            //setupDB();
            
            transactionManager = new TransactionManager();
            orderManager = new OrderManager();
            quoteManager = new QuoteManager();
            positionManager = new PositionManager();
            tickManager = new TickManager();
        } catch(Exception ex){
            ex.printStackTrace();
        }
    }

    public static long updateTime = 60000;

    public static Queue orders = new LinkedBlockingQueue();
    public static Vector positions = new Vector();
    public static Queue history = new LinkedList();
    public static Queue transactions = new LinkedList();
    public static Hashtable registry = new Hashtable();
    
    public static boolean testing = false;
    public static boolean verbose = false;

    public static void reset()
    {
        try {
            orderManager = new OrderManager();
            quoteManager = new QuoteManager();
            orders = new LinkedBlockingQueue();
            history = new LinkedList();
            transactions = new LinkedList();
            positions = new Vector();
            verbose = false;

            clearDB();
            setupDB();
        } catch(Exception ex){

        }
    }

    public static void clearDB()
    {
        try {
            Connection conn = DatabaseManager.getConnection();
            Statement statement = null;
            try {
                statement = conn.createStatement();
                try {statement.executeUpdate("DELETE FROM transactions");} catch(Exception ex) { }
                try {statement.executeUpdate("DELETE FROM bullion");} catch(Exception ex) { }
                try {statement.executeUpdate("DELETE FROM users");} catch(Exception ex) { }
                try {statement.executeUpdate("DELETE FROM accounts");} catch(Exception ex) { }
                try {statement.executeUpdate("DELETE FROM positions");} catch(Exception ex) { }
                try {statement.executeUpdate("DELETE FROM instruments");} catch(Exception ex) { }
            } catch (Exception ex) {
                Log.info(ex);
                ex.printStackTrace();
            }

        } catch (Exception ex) {
            Log.info(ex);
            ex.printStackTrace();
        }
    }

    public static void setupDB()
    {
        try {
            Connection conn = DatabaseManager.getConnection();
            Statement statement = null;
            try {
                statement = conn.createStatement();

                InstrumentManager im = new InstrumentManager();
                //im.connect();

                PositionManager pm = new PositionManager();
                //pm.connect();

                TransactionManager tm = new TransactionManager();
                //tm.connect();

                AccountRegistrar ar = new AccountRegistrar();
                //ar.connect();

                UserRegistrar um = new UserRegistrar();
                //um.connect();

                // TESTS ONLY
                if( SharedGlobals.testing ){
                    statement.executeUpdate("INSERT INTO users VALUES ('bx', '123abc', 'bx@yahoo.com','555-555-5555', '21 Way', 0, 'Canada', 'City', 'X', 'mr', 'udo', 'Ontario', true)");
                    statement.executeUpdate("INSERT INTO users VALUES ('robot', '123abc', 'bx@yahoo.com','555-555-5555', '21 Way', 0, 'Canada', 'City', 'X', 'B', 'X', 'Ontario', false)");
                    statement.executeUpdate("INSERT INTO accounts (name, id, balance, owner, initial) VALUES ('SAVE', 'A12e5eb621dh', 10000, 'bx', 10000)");
                    statement.executeUpdate("INSERT INTO accounts (name, id, balance, owner, initial) VALUES ('SAVE', 'A12e5eb621dg', 100000000, 'robot', 100000000)");
                    statement.executeUpdate("INSERT INTO instruments (symbol, name, description) VALUES ('GOLD', 'Gold Instrument', 'This is a Gold Instrument')");
                    statement.executeUpdate("INSERT INTO instruments (symbol, name, description) VALUES ('-', 'Admin', 'Admin')");
                }
                
            } catch (Exception ex) {
                System.out.println(ex);
                ex.printStackTrace();
            }

        } catch (Exception ex) {
            System.out.println(ex);
            ex.printStackTrace();
        }
    }

    /**
     * @return the orderDesk
     */
    public static TickManager getTickManager() {
        return tickManager;
    }

    /**
     * @param aOrderDesk the orderDesk to set
     */
    public static void setTickManager(TickManager aTickDesk) {
        tickManager = aTickDesk;
    }

    /**
     * @return the orderDesk
     */
    public static OrderManager getOrderManager() {
        return orderManager;
    }

    /**
     * @param aOrderDesk the orderDesk to set
     */
    public static void setOrderManager(OrderManager aOrderDesk) {
        orderManager = aOrderDesk;
    }

    /**
     * @return the quoteManager
     */
    public static QuoteManager getQuoteManager() {
        return quoteManager;
    }

    /**
     * @param aQuoteManager the quoteManager to set
     */
    public static void setQuoteManager(QuoteManager aQuoteManager) {
        quoteManager = aQuoteManager;
    }

    /**
     * @return the positionManager
     */
    public static PositionManager getPositionManager() {
        return positionManager;
    }

    /**
     * @param aPositionManager the positionManager to set
     */
    public static void setPositionManager(PositionManager aPositionManager) {
        positionManager = aPositionManager;
    }

    /**
     * @return the transactionManager
     */
    public static TransactionManager getTransactionManager() {
        return transactionManager;
    }

    /**
     * @param aTransactionManager the transactionManager to set
     */
    public static void setTransactionManager(TransactionManager aTransactionManager) {
        transactionManager = aTransactionManager;
    }

}
